<?php

namespace Admin\Controller;

class MenuController extends PublicController
{
    function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 首页加载
     * Author:边
     */
    public function index()
    {
        $store_id = session("wadmin");
        if ($store_id['store_id'] == 0) {
            $data = '';
        } else {
            $data['dc.store_id'] = $store_id['store_id']; // 店铺ID
        }

        $count = M('dishes_category')->alias("dc")->where($data)->count();
        $Page = new \Think\Page($count, 12);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $Page->setConfig('theme', "<ul class='pagination no-margin pull-right'></li><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li><li><a> %HEADER%  %NOW_PAGE%/%TOTAL_PAGE% 页</a></ul>");
        $show = $Page->show(); // 分页显示输出
        $result = M('dishes_category')->alias("dc")->join("meal_store as s on dc.store_id=s.store_id",'left')->field("dc.*,s.store_name")->where($data)->limit($Page->firstRow . ',' . $Page->listRows)->order('dc.id desc')->select();
        foreach ($result as $key => $value) {
            $result[$key]['create_formart_time'] = date('Y-m-d', $value['create_time']);
            if ($value['modify_time']) {
                $result[$key]['modify_formart_time'] = date('Y-m-d', $value['modify_time']);
            } else {
                $result[$key]['modify_formart_time'] = '暂无修改';
            }

        }

        $this->assign("result", $result);
        $this->assign("page", $show);
        $this->display();
    }

    /**
     * 删除分类
     * Author:边
     */
    public function delMenu()
    {
        //判断这个分类下面是不是有数值
        $categoryId = I('get.id');
        $result = M('dishes')->where("category_id='$categoryId'")->find();

        if($result){
            $this->error("该分类下面有商品不能删除!", U('Admin/Menu/index'));
        }else{
            $result_delete = M('dishes_category')->delete($categoryId);
            if ($result_delete) {
                $this->success("操作成功", U('Admin/Menu/index'));
            } else {
                $this->error("删除失败", U('Admin/Menu/index'));
            }
        }
    }

    /**
     * 分类和新增加载页面
     * Author:边
     */
    public function add_edit()
    {
        $store_id = session("wadmin");
        if ($store_id['store_id'] == 0) {
            $data = '';
            $storeId = 0;
        } else {
            $data['store_id'] = $store_id['store_id']; // 店铺ID
            $storeId = $store_id['store_id']; // 店铺ID
        }

        $id = I('get.id');
        $data['id'] = $id;
        $dishes_category = M('dishes_category')->where($data)->find();
        $store = M('store')->select();
        $this->assign('result', $dishes_category);
        $this->assign('store', $store);
        $this->assign('storeId', $storeId);
        $this->display();
    }

    /**
     * 编辑新增,分类
     * Author:边
     */
    public function editMenu()
    {
        $id = I('request.id');
        $data['name'] = I('post.name');
        $data['description'] = I('post.description');
        $store_id = session("wadmin");
        if ($store_id['store_id'] == 0) {
            $data['store_id'] = I('post.store_id');
        } else {
            $data['store_id'] = $store_id['store_id'];
        }
        if ($id) {
            //修改
            $data['modifier'] = $store_id['user_name'];
            $data['modify_time'] = time();
            $result = M('dishes_category')->where("id='$id'")->save($data);
            if ($result === false) {
                $this->error("操作失败");
            } else {
                $this->success("操作成功",U('Admin/Menu/index'));
            }
        } else {
            //新增
            $data['creater'] = $store_id['user_name'];
            $data['create_time'] = time();
            $result = M('dishes_category')->add($data);
            if ($result) {
                $this->success("操作成功",U('Admin/Menu/index'));
            } else {
                $this->error("操作失败");
            }
        }
    }
}